package com.reshare.controller;

import com.reshare.annotation.Log;
import com.reshare.base.BaseController;
import com.reshare.base.ResultInfo;
import com.reshare.bean.User;
import com.reshare.model.UserModel;
import com.reshare.query.UserQuery;
import com.reshare.service.UserService;
import com.reshare.utils.LoginUserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;


@Controller
@RequestMapping("user")
public class UserController extends BaseController {

    @Autowired
    private UserService userService;

    /**
     * 注册
     * @param userName
     * @param userPwd
     * @return
     */
    @RequestMapping("register")
    @ResponseBody
    public ResultInfo Register(String userName, String userPwd){
        userService.userRegister(userName, userPwd);
        return success("注册成功了");
    }
    @RequestMapping("toRegister")
    public String toRegister(){
        return "user/register";
    }

    @RequestMapping("login")
    @ResponseBody
    public ResultInfo Login(String userName, String userPwd) {
        System.out.println(userName);
        System.out.println(userPwd);
        //实例化对象
        ResultInfo resultInfo = new ResultInfo();
        //调用登录
        UserModel userModel = userService.userLogin(userName, userPwd);
        //将信息传入resultInfo
        resultInfo.setResult(userModel);
        //返回
        return resultInfo;
    }

    /**
     * 修改密码
     * @param req
     * @param oldPwd
     * @param newPwd
     * @param confirmPwd
     * @return
     */
    @RequestMapping("changePwd")
    @ResponseBody
    public ResultInfo change(HttpServletRequest req, String oldPwd, String newPwd, String confirmPwd){
        //实例化对象
        ResultInfo resultInfo = new ResultInfo();
        //获取信息
        Integer userId = LoginUserUtil.releaseUserIdFromCookie(req);
        //调用
        userService.changePwd(userId,oldPwd,newPwd,confirmPwd);
        //返回
        return resultInfo;
    }

    @RequestMapping("setting")
    @ResponseBody
    public ResultInfo changeSetting(HttpServletRequest req,String userName,String email,String phone,String trueName){
        //根据cookie获取id
        Integer userId = LoginUserUtil.releaseUserIdFromCookie(req);
        //调用
        userService.changeUserSetting(userId,userName,email,phone,trueName);
        //返回
        return success("修改成功");
    }

    //跳转页面
    @RequestMapping("toSettingPage")
    public String setting(HttpServletRequest req){
        //从Cookie获取用户的ID
        Integer userId = LoginUserUtil.releaseUserIdFromCookie(req);
        //调用方法查询用户信息
        User user = userService.selectByPrimaryKey(userId);
        //存储数据
        req.setAttribute("user",user);
        //返回页面
        return "user/setting";
    }

    //跳转页面
    @RequestMapping("toPasswordPage")
    public String password(){
        return "user/password";
    }



    /**
     * 转发到user界面
     * @author WangYueHu
     * @date 2021/9/1 11:11
     * @return java.lang.String
     */
    @RequestMapping("index")
    public String index(){
        //转发
        return "user/user";
    }



    /**
     * 查询信息
     * @author WangYueHu
     * @date 2021/9/1 11:44
     * @return java.util.List<java.util.Map<java.lang.String,java.lang.Object>>
     */
    @RequestMapping("list")
    @ResponseBody
    public Map<String,Object> selUser(UserQuery query){
        Map<String,Object> map=userService.selUser(query);
        return map;
    }


    /**
     * 判断添加或修改，转发user添加视图
     * @author WangYueHu
     * @date 2021/9/1 19:25
     * @param userId
     * @param model
     * @return java.lang.String
     */
    @RequestMapping("addOrUpdateDialog")
    public String addOrUpdate(Integer userId, Model model){
        if (userId!=null){
            //通过userId查到该选中的用户对象
            User user =userService.selectByPrimaryKey(userId);
            //存储
            model.addAttribute(user);
        }
        return "user/add_update";
    }


    /**
     * 添加用户
     * @author WangYueHu
     * @date 2021/9/1 20:47
     * @param user
     * @return com.reshare.base.ResultInfo
     */
    @RequestMapping("save")
    @ResponseBody
    public ResultInfo addUser(User user){
        //调用添加方法
        userService.saveUser(user);

        //返回目标对象
        return success("添加成功了");
    }


    /**
     * 更新用户
     * @author WangYueHu
     * @date 2021/9/1 21:20
     * @param user
     * @return com.reshare.base.ResultInfo
     */
    @RequestMapping("update")
    @ResponseBody
    public ResultInfo updateUser(User user){
        //调用修改方法
        userService.updateUser(user);

        //返回目标对象
        return success("更新成功了");
    }


    /**
     * 删除用户
     * @author WangYueHu
     * @date 2021/9/2 9:37
     * @param ids
     * @return com.reshare.base.ResultInfo
     */
    @RequestMapping("delete")
    @ResponseBody
    public ResultInfo deleteUser(Integer[] ids){
        //调用删除方法
        userService.deleteUser(ids);

        //返回目标对象
        return success("删除成功了");
    }


}
